package com.qlyl.common.util;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStreamReader;

public class MakeHtmlUtil {
	public static String readFile(String filePathAndName) {
		String fileContent = "";
		try {
			File f = new File(filePathAndName);
			if (f.isFile() && f.exists()) {
				InputStreamReader read = new InputStreamReader(new FileInputStream(f), "UTF-8");
				BufferedReader reader = new BufferedReader(read);
				String line;
				while ((line = reader.readLine()) != null) {
					// 将读取到的字符拼接
					fileContent += line;
				}
				read.close();
			}
		} catch (Exception e) {
			System.out.println("读取文件内容操作出错");
			e.printStackTrace();
		}
		// System.out.println("fileContent:"+fileContent);
		return fileContent;
	}

	/**
	 * @Title: MakeHtml @Description: 创建html @param filePath 设定模板文件 @param
	 *         disrPath 生成html的存放路径 @param fileName 生成html名字 @return void
	 *         返回类型 @throws
	 */
	public static void MakeHtml(String htmlCountent, String disrPath, String fileName, String Mbpath) {
		try {
			// System.out.print(htmlCountent);
			String templateContent = "";// htmlCountent;
			FileInputStream fileinputstream = new FileInputStream(Mbpath);// 读取模板文件
			int lenght = fileinputstream.available();
			byte bytes[] = new byte[lenght];
			fileinputstream.read(bytes);
			fileinputstream.close();
			templateContent = new String(bytes);
			// System.out.print(templateContent);
			templateContent = templateContent.replaceAll("##count##", htmlCountent);
			// System.out.print(templateContent);
			String fileame = fileName + ".html";
			File dirs = new File(disrPath);
			if (dirs.exists() == false)
				dirs.mkdirs();
			fileame = disrPath + "/" + fileame;// 生成的html文件保存路径。
			FileOutputStream fileoutputstream = new FileOutputStream(fileame);// 建立文件输出流
			System.out.print("文件输出路径:");
			System.out.print(fileame);
			byte tag_bytes[] = templateContent.getBytes();
			fileoutputstream.write(tag_bytes);
			fileoutputstream.close();
		} catch (Exception e) {
			System.out.print(e.toString());
		}
	}

	/**
	 * 绑定标题生成HTML重载方法
	 * 
	 * @Title: MakeHtml @Description: 创建html @param filePath 设定模板文件 @param
	 *         disrPath 生成html的存放路径 @param fileName 生成html名字 @return void
	 *         返回类型 @throws
	 */
	public static void MakeHtml(String htmlCountent, String title, String disrPath, String fileName, String Mbpath) {
		try {
			// System.out.print(htmlCountent);
			String templateContent = "";// htmlCountent;
			FileInputStream fileinputstream = new FileInputStream(Mbpath);// 读取模板文件
			int lenght = fileinputstream.available();
			byte bytes[] = new byte[lenght];
			fileinputstream.read(bytes);
			fileinputstream.close();
			templateContent = new String(bytes);
			// System.out.print(templateContent);
			templateContent = templateContent.replaceAll("<title></title>", "<title>" + title + "</title>");
			templateContent = templateContent.replaceAll("##count##", htmlCountent);
			// System.out.print(templateContent);
			String fileame = fileName + ".html";
			File dirs = new File(disrPath);
			if (dirs.exists() == false)
				dirs.mkdirs();
			fileame = disrPath + "/" + fileame;// 生成的html文件保存路径。
			FileOutputStream fileoutputstream = new FileOutputStream(fileame);// 建立文件输出流
			System.out.print("文件输出路径:");
			System.out.print(fileame);
			byte tag_bytes[] = templateContent.getBytes();
			fileoutputstream.write(tag_bytes);
			fileoutputstream.close();
		} catch (Exception e) {
			System.out.print(e.toString());
		}
	}

	public static void MakeHtmlJavaScript(String path, String Mbpath) {
		try {
			File f = new File(path);
			if (f.isFile() && f.exists()) {
				String templateContent = "";// htmlCountent;
				FileInputStream fileinputstream = new FileInputStream(Mbpath);// 读取模板文件
				int lenght = fileinputstream.available();
				byte bytes[] = new byte[lenght];
				fileinputstream.read(bytes);
				fileinputstream.close();
				templateContent = new String(bytes);
				String html = readFile(path);
				if(html != null && html.indexOf("</body>") != -1){
					String body = html.split("<body>")[1].split("</body>")[0];
					templateContent = templateContent.replaceAll("##count##", body);
//					templateContent = html.split("<script")[0];
//					templateContent += html.split("</script>")[1];
					FileOutputStream fileoutputstream = new FileOutputStream(path);// 建立文件输出流
					System.out.print("文件输出路径:");
					System.out.print(path);
					byte tag_bytes[] = templateContent.getBytes();
					fileoutputstream.write(tag_bytes);
					fileoutputstream.close();
				}
//					templateContent = templateContent.split("</title>")[0]
//							+ "</title><script type=\"text/javascript\">window.onload=function(){var data={ \"clientHeight\":document.body.clientHeight,"
//							+ "\"scrollHeight\": document.body.scrollHeight,\"scrollTop\":document.body.scrollTop}"
//							+ "window.parent.postMessage(JSON.stringify(data), \"*\");}</script><html>"
//							+ templateContent.split("</title>")[1];
//					templateContent = templateContent.replaceAll("</script><html>", "</script>");
				 
			}
		} catch (Exception e) {
			System.out.print(e.toString());
		}
	}

	public static void main(String[] args) {
		MakeHtmlJavaScript("/Users/xr/Downloads/7.21/wz/pageArticle/179/editHtml789359/index.html","/Users/xr/Desktop/mb.html");
	}

}
